package com.zzty.zzlp.dao;

import com.zzty.zzlp.entity.Chat;
import com.zzty.zzlp.entity.ChatBuffer;
import com.zzty.zzlp.entity.User;
import com.zzty.zzlp.entity.User_info;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface ChatMapper {

    @Insert("insert into chat (fromId,toId,time,msg,isGroup,state,groupId,type) values (#{fromId},#{toId},#{time},#{msg},#{isGroup},#{state},#{groupId},#{type})")
    public void createChat(Chat chat);

    @Select("select * from chat where toId=#{userId} and isGroup=0")
    public List<Chat> searchChat(Integer userId);

    @Select("select * from user where state=1")
    public List<User> searchUsers();

    @Select("select * from chat where toId=#{toId} and fromId=#{fromId} and isGroup=0 order")
    public Chat searchLast(Chat chat);

    @Select("select * from chat where time in (select MAX(time) from chat where (fromId=#{fromId1} AND toId=#{fromId2} and isGroup=0)or (fromId=#{fromId2} AND toId=#{fromId1} and isGroup=0))")
    public List<Chat> searchFromMessage(@Param("fromId1") Integer fromId1, @Param("fromId2") Integer fromId2);

    @Select("select * from chat where (fromId=#{fromId1} and toId=#{fromId2} and type='text' and isGroup=0) or (fromId=#{fromId2} and toId=#{fromId1} and type='text' and isGroup=0) ")
    public List<Chat> searchAllMessage(@Param("fromId1") Integer fromId1, @Param("fromId2") Integer fromId2);

    @Select("select * from chat where fromId=#{fromId1} and toId=#{fromId2} and state=0  and isGroup=0 order by time ")
    public List<ChatBuffer> searchMessageCount(@Param("fromId1") Integer fromId1, @Param("fromId2") Integer fromId2);

    @Update("update chat set state=1 where (fromId=#{fromId1} and toId=#{fromId2} and isGroup=0)")
    public void readMessage(@Param("fromId1") Integer fromId1, @Param("fromId2") Integer fromId2);

    @Select("select * from chat where time in (SELECT MAX(time) from chat where groupId = #{groupId}) LIMIT 0,1")
    public Chat searchGroupLastMsg(@Param("groupId") Integer groupId);

    @Select("select * from chat where groupId=#{groupId} and toId=#{userId} and state=0 order by time ")
    public List<ChatBuffer> searchGroupMessageCount(@Param("groupId") Integer groupId, @Param("userId") Integer userId);

    @Update("update chat set state=1 where (groupId=#{groupId} and toId=#{userId})")
    public void readGroupMessage(@Param("groupId") Integer groupId, @Param("userId") Integer userId);

    @Select("select * from user_info where user_id=#{userId}")
    public User_info searchUserInfo(@Param("userId") Integer userId);

    @Select("select * from user where id=#{userId}")
    public User searchUserById(@Param("userId") Integer userId);



}
